package com.ophiux.FeverManagement.aplication.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.editPatientTreatmentRequest;
import com.ophiux.FeverManagement.aplication.bean.editPatientTreatmentResponse;
import com.ophiux.FeverManagement.aplication.service.IEditPatientTreatmentAppPort;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatment;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentDetail;
import com.ophiux.FeverManagement.domain.entity.RbTreatment;
import com.ophiux.FeverManagement.domain.repository.IDoctorRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentDetailRepository;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentRepository;
import com.ophiux.FeverManagement.domain.repository.ITreatmentRepository;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.AssemblyFactory;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class EditPatientTreatmentAppService implements IEditPatientTreatmentAppPort {
	@Autowired
	IPatientTreatmentRepository patientTreatmentRepository;
	@Autowired
	IPatientTreatmentDetailRepository patientTreatmentDetailRepository;
	@Autowired
	IDoctorRepositories doctorRepositories;
	@Autowired
	IPatientRepositories patientRepositories;
	@Autowired
	ITreatmentRepository treatmentRepository;

	@Override
	public Result<editPatientTreatmentResponse> execute(editPatientTreatmentRequest request) {
		editPatientTreatmentResponse resp = new editPatientTreatmentResponse();
		try {
			if (!request.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}

			RbPatientTreatment model = patientTreatmentRepository.selectOneByKey(request.getPATIENT_TREATMENT_ID());
			if (model == null) {
				return Result.fail(ResultEnum.NotFoundPATIENT_TREATMENT);
			}
			
			RbTreatment treatment = treatmentRepository.selectOneByKey(request.getTREATMENT_ID());
			if(treatment == null) {
				return Result.fail(ResultEnum.NotFoundTreatment);
			}
			
			model.setTREATMENT_ADVISE(request.getTREATMENT_ADVISE());
			model.setTREATMENT_TITLE(treatment.getTITLE());
			
			
			List<RbPatientTreatmentDetail> model1 = AssemblyFactory.instance().parse(request.getMEDICINE(), RbPatientTreatmentDetail.class);
			patientTreatmentRepository.save(model, model1);
			
			resp.setRESULT("1");
		} catch (Exception e) {
			log.info("EditPatientTreatmentAppService=》execute(),程序异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success(resp);
	}

}
